// 暂时用这个单例来解决,后面该用 redis
class CreateMode {
  asyncNums = 0; // 初始并发数
  isOpen = true; // 是否开启上传日志: 默认不开启-服务重启以后一直报错
  logTeamsSendLast = 0; // 0: 可以发送  1: 已发送过了
  isSizeMax = false; // 是否算作大文件?
  ignoreErrors = []; // 过滤上报信息关键字
  constructor(val?: any) {
    if (val) {
      this.asyncNums = val.asyncNums || 0;
    }
  }
  // getName() {
  //   return this.name;
  // }
}

/**
 * @description: 暂时拦截域名，后面使用请求头拦截
 */
export const interceptDomain = (event: any) => {
  // 用于拦截非法用户请求
  const originHostList = [
    "https://test-bdc.btcdana.org",
    "https://pre-bdc.btcdana.org",
    "https://bdc.btcdana.org",
    "https://bdc-sg.btcdana.org", // 新加坡
    "https://rich.okmax.vip",
    "new-rich.pre-happy2012.com", // 预发
    "new-rich.happy2012.com", // 线上
    "http://localhost:3000",
    "http://localhost:3001",
  ];

  const origin = getHeader(event, "origin");
  console.log("origin:", origin);
  // const referer = getHeader(event, "referer"); // console.log("referer:", referer);
  if (!originHostList.includes(origin || "")) {
    return {
      code: 403,
      message: "凭证失效,暂无权限",
      data: false,
    };
  }
};

// 代理实现单例模式
export const ProxyMode = (function () {
  let instance: any = null;
  return function (val?: any) {
    if (!instance) {
      instance = new CreateMode(val);
    }
    return instance;
  };
})();

/**
 * @description: 成功
 */
export const success = (data: any = true) => {
  // 防止并发过大
  const g = ProxyMode();
  g.asyncNums--;
  // 返回数据
  return {
    code: 200,
    message: "ok",
    data: data,
  };
};
/**
 * @description: 失败
 */
export const fail = (err: any = false) => {
  // 防止并发过大
  const g = ProxyMode();
  g.asyncNums--;
  // 返回数据
  return {
    code: 400,
    message: err,
    data: false,
  };
};
